home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Crash Proofing?
- Date: 11 Mar 1996 08:47:21 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4i1at9$p0t@umbc9.umbc.edu>
- References: <4hsfje$rbr@uwm.edu>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Peter J Kleczka <peterk@alpha2.csd.uwm.edu> wrote:
- |> I'm new to C programing and am trying to re-write a program
- |> I did in pascal in C. When I wrote the pascal program I got
- |> caught up in the details of getting the program to work and
- |> consequentially readability of the code and user-friendlyness
- |> suffered......
- |>
- |> The program I'm working on (below) works fine as long
- |> as the user enters an integer.....but it gets caught in an
- |> endless loop between the functions: firstmenu() and firstchoice()
- |> when I enter, say, 5.5 instead of an integer
- |>
- |> it's unlikely that the user would enter anything but an integer
- |> when presented with integer choices...but I want to make the
- |> code uncrashable .....what can I do if anything to make it
- |> so that the program doesnt do wierd things if the user enters
- |> the wrong thing here?
-
- [code deleted]
-
- |> firstchoice(){ /* get users choice and branch to appropos function */
- |>
- |> unsigned choice;
- |>
- |> scanf ("%d", &choice);
-
- [rest deleted]
-
- Your code was long so I removed all but what I needed. First of all 'choice'
- is declared as an unsigned int, but you use the normal int specifier within
- scanf(). So I'd change %d to %u...Furthermore, don't even use scanf() at all.
- It's terrible in loops and you seem to have stumbled across this with the
- problem you're having. Instead grab the text with fgets() and then strip off
- what you need with sscanf(). This also makes for much easier and well written
- error checking capabilities.
-
- Check up on the FAQ at rtfm.mit.edu which has complete details on other
- nasty things scanf() does and why other options are usually preferable.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-